import sys
from setting import *
from store.intents import slice_types
import utils.ryu_interface as ryu_if

def get_ave_bandwidth_efficiency():
    if ryu_if.ryu_interface == None:
        print("RPYC connection to RYU hasn't establish.",file=sys.stderr)
        raise Exception(ValueError)
    net_topo = ryu_if.ryu_interface.get_net_topo()
    ave_efficiency = 0
    links_num = 0
    for (src,dst,link_info) in net_topo.edges(data="link_info"):
        # print(f'{src}->{dst}:{link_info.throughput/link_info.bandwidth:.4f},{link_info.throughput/1e6:.4f}mbps,{link_info.bandwidth/1e6:.0f}mbps')
        link_util = link_info.throughput/link_info.bandwidth
        ave_efficiency += link_util if link_util <=1 else 1
        links_num += 1
    ave_efficiency /= links_num
    return ave_efficiency


def get_ave_loss():
    if ryu_if.ryu_interface == None:
        print("RPYC connection to RYU hasn't establish.",file=sys.stderr)
        raise Exception(ValueError)
    net_topo = ryu_if.ryu_interface.get_net_topo()
    ave_loss = 0
    links_num = 0
    for (_,_,link_info) in net_topo.edges(data="link_info"):
        ave_loss += link_info.loss
        links_num += 1
    return ave_loss/links_num

def get_slices_weights():
    if ryu_if.ryu_interface == None:
        print("RPYC connection to RYU hasn't establish.",file=sys.stderr)
        raise Exception(ValueError)
    weights = ryu_if.ryu_interface.get_slices_weights()
    return [{"name":slice_name, "value":weights[slice_id]} for slice_id,slice_name in slice_types.items()]